package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConst;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	OrderSettingDao orderSettingDao;

	@Autowired
	MemberDao memberDao;

	@Autowired
	OrderDao orderDao;

	@Override
	@Transactional
	public Result addOrder(Map<String, String> map){

		String telephone = map.get("telephone");
		String name = map.get("name");
		String setmealId = map.get("setmealId");
		String sex = map.get("sex");
		String idCard = map.get("idCard");
		String orderType = map.get("orderType");
		String orderDateStr = map.get("orderDate");

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		OrderSetting orderSetting = null;

		//判断该日期是否进行日期设置
		try {
			orderSetting = orderSettingDao.findByDate(sdf.parse(orderDateStr));

		} catch (ParseException e) {
			e.printStackTrace();
		}
		if(orderSetting == null){
			return  new Result(false, MessageConst.SELECTED_DATE_CANNOT_ORDER);
		}

		//判断预约人数是否大于最大预约人数
		if(orderSetting.getReservations()> orderSetting.getNumber()){
			return  new Result(false, MessageConst.ORDER_FULL);
		}

		//判断是否是会员
		Member member = memberDao.findByTelephone(telephone);

		if(member != null){

			//不存在  更新会员信息
			member.setName(name);
			member.setSex(sex);
			member.setIdCard(idCard);

			memberDao.edit(member);

			//判断是否重复预约,重复返回错误页面

			Order conditionOrder  =new Order();
			conditionOrder.setMemberId(member.getId());
			conditionOrder.setSetmealId(Integer.valueOf(setmealId));
			try {
				conditionOrder.setOrderDate(sdf.parse(orderDateStr));
			} catch (ParseException e) {
				e.printStackTrace();
			}

			long count = orderDao.findByCondition(conditionOrder);

			if(count>0){
				return  new Result(false, MessageConst.HAS_ORDERED);

			}

		}else {//自动注册会员  新增

			member = new Member();
			member.setIdCard(idCard);
			member.setSex(sex);
			member.setName(name);
			member.setPhoneNumber(telephone);
			member.setRegTime(new Date());

			//主键回显,用于预约信息order中的id
			memberDao.add(member);

		}

		//添加预约信息  order
		Order order = new Order();
		order.setMemberId(member.getId());
		try {
			order.setOrderDate(sdf.parse(orderDateStr));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		order.setOrderType(orderType);
		order.setSetmealId(Integer.valueOf(setmealId));
		//未到诊  等就诊时医生修改状态
		order.setOrderStatus(Order.ORDERSTATUS_NO);

		orderDao.addOrder(order);


		//更新预约设置信息,已预约人数+1
		orderSetting.setReservations(orderSetting.getReservations()+1);

		orderSettingDao.edit(orderSetting);


		return new Result(true,MessageConst.ORDERSETTING_SUCCESS,order);
	}

	@Override
	public Map<String, Object> findById(Integer id) {


		return orderDao.findById(id);

	}
}
